package com.test;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;


public class SyncPost {
    private StringBody mParams = null;
	private DefaultHttpClient mHttpClient;
	static SyncPost post;
    public static SyncPost getPostInstance() {
    	if(post == null){
    		post = new SyncPost();
    	}
        return post;
    }
    
    public SyncPost() {
    	HttpParams params = new BasicHttpParams();
	    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
	    mHttpClient = new DefaultHttpClient(params);
    }
    
    public SyncPost addParams(String params) {
    	try {
			mParams = new StringBody(params);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return this;
    }
    
    
    public String postJson(String url) throws ClientProtocolException, IOException{
    	HttpPost httppost = new HttpPost(url);
    	
        HttpResponse httpResponse = null;
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        multipartEntity.addPart("params", mParams);

        httppost.setEntity(multipartEntity);

       // mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
        httpResponse =  mHttpClient.execute(httppost);
        
        HttpEntity r_entity = httpResponse.getEntity();
        String responseString = EntityUtils.toString(r_entity);
        String str = responseString;
        
        return responseString;
    }
}